package com.turing.post.common.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.CommonApplicationException;
import com.turing.post.common.util.SysOpRole;
import com.turing.post.common.util.SysOpRoleDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 16, 2012
 */
@Stateless(mappedName = "common.impl.SysOpRoleDaoImpl")
@Remote
@Local
public class SysOpRoleDaoImpl implements SysOpRoleDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteSysOpRole(Integer roleId) throws PCPSApplicationException {
		SysOpRole eo = getSysOpRoleByRoleId(roleId);
		if (eo != null) {
			GeneralDao.remove(_em, eo);
		}
	}

	@Override
	public BigInteger getMaxRoleId() {
		Query query = _em
				.createNativeQuery("select nvl(max(roleid),0)+1 role from sysoprole t");
		return ((BigDecimal) query.getSingleResult()).toBigInteger();
	}

	@Override
	public SysOpRole getSysOpRoleByRoleId(Integer roleId) {
		return _em.find(SysOpRole.class, roleId);
	}

	@Override
	public void insertSysOpRole(PCPSWebInput input, SysOpRole sysOpRole)
			throws PCPSApplicationException {
		sysOpRole.setRoleId(getMaxRoleId());
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new OrganApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "修改用户传入对象为空!");
		}
		sysOpRole.setIssueCompanyNo(input.getOperBranchNo().substring(0, 6));
		GeneralDao.insert(_em, sysOpRole);
	}

	@Override
	public void updateSysOpRole(SysOpRole sysOpRole)
			throws PCPSApplicationException {
		SysOpRole _sysOpRole = getSysOpRoleByRoleId(sysOpRole.getRoleId()
				.intValue());
		if (_sysOpRole == null) {
			throw new OrganApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "待修改角色数据库中不存在!");
		}
		_sysOpRole.setRoleName(sysOpRole.getRoleName());
//		_sysOpRole.setRoleBranchType(sysOpRole.getRoleBranchType());
		_sysOpRole.setRemark(sysOpRole.getRemark());
		GeneralDao.update(_em, _sysOpRole);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpRole> getSysOpRoleList(PCPSWebInput input,
			String brchType, Integer page) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em
				.createNativeQuery(
						"select * from SysOpRole t "
								+ "where t.RoleId <> 0 and IssueCompanyNo='"
								+ input.getOperBranchNo().substring(0, 6)
								+ "' and (?1 is null or t.rolebranchtype=?1) order by t.roleBranchType,RoleId",
						SysOpRole.class).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		query.setParameter(1, brchType);
		return query.getResultList();
	}

	@Override
	public BigInteger getSysOpRoleTotal(PCPSWebInput input, String brchType) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return BigInteger.ZERO;
		}
		Query query = _em
				.createNativeQuery("select nvl(count(*),0) count from SysOpRole t "
						+ " where t.RoleId <> 0 and IssueCompanyNo='"
						+ input.getOperBranchNo().substring(0, 6)
						+ "'"
						+ " and (?1 is null or t.rolebranchtype=?1)");
		query.setParameter(1, brchType);
		return ((BigDecimal) query.getSingleResult()).toBigInteger();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpRole> getSysOpRoleList(PCPSWebInput input,
			String roleBranchType) {
		String sql = "select * from SysOpRole t where t.RoleId <> 0 and (?1 is null or roleBranchType=?1) and IssueCompanyNo='"
				+ input.getOperBranchNo().substring(0, 6)
				+ "' order by t.roleBranchType,RoleId";
		return _em.createNativeQuery(sql, SysOpRole.class).setParameter(1,
				roleBranchType).getResultList();
	}
}
